package samples.d2.polygons 
{
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.setInterval;
	import samples.BaseSample;
	import shapes.shapes2D.distribution.Circular;
	import shapes.shapes2D.distribution.Frame;
	import shapes.shapes2D.polygon.ConvexHull;
	import shapes.shapes2D.polygon.EarCutting;
	import shapes.shapes2D.polygon.Edge;
	import shapes.shapes2D.polygon.Polygon;
	import shapes.shapes2D.polygon.Simplify;
	import shapes.shapes2D.polygon.Skeleton;
	import shapes.shapes2D.polygon.triangle.Triangle;
	import shapes.shapes2D.polygon.Triangulation;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Polygon7 extends BaseSample
	{
		
		private var poly:Polygon;
		public function Polygon7() 
		{
			
			poly = new Coq( sw/2, sh/2, 3 );
			
			var ce:Point = poly.centroid;
			
			graphics.lineStyle( 10, 0x003399, .5 );
			plotLines( poly.points );
			
			graphics.lineStyle( 1, 0xFF0000 );
			plotPoints( poly.points );
			plotLines( poly.points );
			
			var points:Vector.<Point> = Simplify.simplifyLang( poly.points, 20, 10);
			
			graphics.lineStyle( 5, 0xFFFF00 );
			plotPoints( points );
			graphics.lineStyle( 2, 0xFFFF00 );
			plotLines( points );
			
			
			//graphics.lineStyle( 2, 0xFF00FF );
			//points = Triangulation.process( ConvexHull.process( points ) );
			//for ( var i:int = 0; i < points.length; i += 3 )
			//{
				//graphics.moveTo( points[ i ].x, points[ i ].y );
				//graphics.lineTo( points[ i+1 ].x, points[ i+1 ].y );
				//graphics.lineTo( points[ i+2 ].x, points[ i+2 ].y );
				//graphics.lineTo( points[ i ].x, points[ i ].y );
			//}
			//
			//var tris:Vector.<Triangle> = EarCutting.cut( ConvexHull.process( points ) );
			//for each( var t:Triangle in tris ) t.draw( graphics ); 
			
		}
		
		private function addPoint():void 
		{
			var i:int = 10;
			while ( i-- )
			{
				var p:Point = new Point( Math.random() * sw, Math.random() * sh );
				
				graphics.lineStyle( 4, 0xFF0000 );
				if ( poly.contains( p ) )
				{
					graphics.lineStyle( 4, 0x00FF00 );
				}
				graphics.moveTo( p.x, p.y );
				graphics.lineTo( p.x+.5, p.y );
			}
			
		}
		
		private function plotPoints( points:Vector.<Point>):void
		{
			
			var p:Point = points[ 0 ];
			for each( p in points )
			{
				graphics.drawCircle( p.x, p.y, 2 );
			}
			
		}
		
		private function plotLines( points:Vector.<Point>):void
		{
			if ( points.length == 0 ) return;
			var p:Point = points[ 0 ];
			graphics.moveTo( p.x, p.y );
			
			for each( p in points )
			{
				
				graphics.lineTo( p.x, p.y );
				
			}
			graphics.lineTo( points[ 0 ].x, points[ 0 ].y );
			
		}
		
		private function plotEdges( edges:Vector.<Edge>):void
		{
			if ( edges.length == 0 ) return;
			
			var e:Edge = edges[ 0 ];
			
			for each( e in edges )
			{
				
				graphics.moveTo( e.p0.x, e.p0.y );
				graphics.lineTo( e.p1.x, e.p1.y );
				
			}
			
		}
		private function plotNormals( edges:Vector.<Edge>, left:Boolean = true ):void
		{
			if ( edges.length == 0 ) return;
			
			var e:Edge;
			var n:Edge;
			
			for each( e in edges )
			{
				if ( left ) n = e.leftNormal;
				else n = e.rightNormal;
				
				graphics.moveTo( n.p0.x, n.p0.y );
				graphics.lineTo( n.p1.x, n.p1.y );
				
			}
			
		}
		
	}

}